//File:TrayIcon.cpp
//The functions of the tray icon control
#include "stdafx.h"
#include <Windows.h>
#include "TrayIcon.h"
#include "ShellAPI.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

TrayIcon::TrayIcon()
	: m_hWnd(0), m_id(0)
{

}

TrayIcon::~TrayIcon()
{
	if (this->m_hWnd != 0)
	{
		this->Destroy();
	}
}

bool TrayIcon::Create(HWND hWnd, UINT uID, HICON hIcon, LPCTSTR tip, UINT nCallBackMessage)
{
	NOTIFYICONDATA nidata;
	memset(&nidata, 0, sizeof(nidata));
	nidata.cbSize = sizeof(nidata);
	nidata.hIcon = hIcon;
	nidata.hWnd = hWnd;
	if (tip != 0)
		_tcscpy(nidata.szTip, tip);
	else
		nidata.szTip[0] = '\x0';
	nidata.uCallbackMessage = nCallBackMessage;
	nidata.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
	nidata.uID = uID;
	BOOL bRes = Shell_NotifyIcon(NIM_ADD, &nidata);

	if (bRes)
	{
		m_hWnd = hWnd;
		m_id = uID;
		return true;
	}
	else
	{
		return false;
	}
}

void TrayIcon::Destroy()
{
	NOTIFYICONDATA nidata;
	memset(&nidata, 0, sizeof(nidata));
	nidata.cbSize = sizeof(nidata);
	nidata.hWnd = m_hWnd;
	nidata.uID = m_id;
	Shell_NotifyIcon(NIM_DELETE, &nidata);
}

bool TrayIcon::SetIcon(HICON hIcon)
{
	NOTIFYICONDATA nidata;
	memset(&nidata, 0, sizeof(nidata));
	nidata.cbSize = sizeof(nidata);
	nidata.hIcon = hIcon;
	nidata.hWnd = m_hWnd;
	nidata.uFlags = NIF_ICON;
	nidata.uID = m_id;
	BOOL bRes = Shell_NotifyIcon(NIM_MODIFY, &nidata);

	return bRes != FALSE;
}

bool TrayIcon::SetTip(LPCTSTR tip)
{
	NOTIFYICONDATA nidata;
	memset(&nidata, 0, sizeof(nidata));
	nidata.cbSize = sizeof(nidata);
	nidata.hWnd = m_hWnd;
	if (tip != 0)
		_tcscpy(nidata.szTip, tip);
	else
		nidata.szTip[0] = '\x0';
	nidata.uFlags = NIF_TIP;
	nidata.uID = m_id;
	BOOL bRes = Shell_NotifyIcon(NIM_MODIFY, &nidata);

	return bRes != FALSE;
}
